package com.ybkj.daijia.server.amqp;

import com.google.gson.Gson;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.server.event.model.zhuan.ZhuanOrderCreateEvent;
import com.ybkj.daijia.server.event.model.zhuan.ZhuanOrderCreatePaidanEvent;
import com.ybkj.daijia.server.event.model.zhuan.ZhuanOrderFillEvent;
import com.ybkj.daijia.server.event.model.zhuan.ZhuanOrderRecordEvent;
import com.ybkj.daijia.server.event.model.zhuanxian.ZhaunxianOrderArriveEvent;
import com.ybkj.daijia.server.event.model.zhuanxian.ZhuanxianOrderAcceptEvent;
import com.ybkj.daijia.server.event.model.zhuanxian.ZhuanxianOrderCancelEvent;
import com.ybkj.daijia.server.event.model.zhuanxian.ZhuanxianOrderCreateEvent;
import com.ybkj.daijia.server.event.model.zhuanxian.ZhuanxianOrderRecordEvent;
import com.ybkj.daijia.server.event.model.zhuanxian.ZhuanxianOrderRefuseEvent;
import com.ybkj.daijia.server.event.model.zhuanxian.ZhuanxianOrderRunEvent;
import com.ybkj.daijia.serverUtils.SettingUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class QueueOneLitener implements MessageListener {

    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    @Autowired
    private SettingUtils settingUtils;


    @Override
    public void onMessage(Message message) {
        String content = null;

        try {
            Setting setting = settingUtils.get();
            if (log.isInfoEnabled()) {
                log.info("mq 得到消息了---------------------------------------");
            }
            content = new String(message.getBody(), "utf-8");

            Gson gson = new Gson();
            PushMessage pushMessage = gson.fromJson(content, PushMessage.class);
            String appKey = pushMessage.getAppKey();

            if (setting.getSecKey().equals(appKey)) {
                if (pushMessage.getOrderType().equals(PushMessage.ORDERTYPE_ZHUANCHE)) {//专车
                    if (pushMessage.getPushType().equals(PushMessage.PUSHTYPE_NEWORDER)) {//新单
                        //快车下单自动派单 触发
                        ZhuanOrderCreateEvent createEvent = new ZhuanOrderCreateEvent(
                            pushMessage.getTargetId(), null);
                        applicationEventMulticaster.multicastEvent(createEvent);

                        ZhuanOrderRecordEvent orderRecordEvent = new ZhuanOrderRecordEvent(
                            pushMessage.getTargetId());
                        applicationEventMulticaster.multicastEvent(orderRecordEvent);
                    } else if (pushMessage.getPushType()
                        .equals(PushMessage.PUSHTYPE_NEWORDER_PAIDAN)) {
                        ZhuanOrderCreatePaidanEvent orderCreatePaidanEvent = new ZhuanOrderCreatePaidanEvent(
                            pushMessage.getTargetId(), pushMessage.getEmployId());
                        applicationEventMulticaster.multicastEvent(orderCreatePaidanEvent);

                        ZhuanOrderRecordEvent orderRecordEvent = new ZhuanOrderRecordEvent(
                            pushMessage.getTargetId());
                        applicationEventMulticaster.multicastEvent(orderRecordEvent);
                    } else if (pushMessage.getPushType().equals(PushMessage.PUSHTYPE_BUDAN)) {
                        ZhuanOrderFillEvent fillEvent = new ZhuanOrderFillEvent(
                            pushMessage.getTargetId(), pushMessage.getEmployId());
                        applicationEventMulticaster.multicastEvent(fillEvent);

                        ZhuanOrderRecordEvent orderRecordEvent = new ZhuanOrderRecordEvent(
                            pushMessage.getTargetId());
                        applicationEventMulticaster.multicastEvent(orderRecordEvent);
                    }
                } else if (pushMessage.getOrderType().equals(PushMessage.ORDERTYPE_ZHUANXIAN)) {//专线
                    if (pushMessage.getPushType().equals(PushMessage.PUSHTYPE_NEWORDER)) {//新单
                        ZhuanxianOrderCreateEvent createEvent = new ZhuanxianOrderCreateEvent(
                            pushMessage.getTargetId());
                        applicationEventMulticaster.multicastEvent(createEvent);

                        ZhuanxianOrderRecordEvent recordEvent = new ZhuanxianOrderRecordEvent(
                            pushMessage.getTargetId());
                        applicationEventMulticaster.multicastEvent(recordEvent);
                    } else if (pushMessage.getPushType()
                        .equals(PushMessage.PUSHTYPE_ACCEPT)) {//接受订单
                        ZhuanxianOrderAcceptEvent acceptEvent = new ZhuanxianOrderAcceptEvent(
                            pushMessage.getTargetId(), pushMessage.getEmployId());
                        applicationEventMulticaster.multicastEvent(acceptEvent);
                    } else if (pushMessage.getPushType().equals(PushMessage.PUSHTYPE_ARRIVE)) {//到达
                        ZhaunxianOrderArriveEvent arriveEvent = new ZhaunxianOrderArriveEvent(
                            pushMessage.getTargetId());
                        applicationEventMulticaster.multicastEvent(arriveEvent);
                    } else if (pushMessage.getPushType().equals(PushMessage.PUSHTYPE_RUN)) {//出发
                        ZhuanxianOrderRunEvent runEvent = new ZhuanxianOrderRunEvent(
                            pushMessage.getTargetId());
                        applicationEventMulticaster.multicastEvent(runEvent);
                    } else if (pushMessage.getPushType().equals(PushMessage.PUSHTYPE_REFUSE)) {//拒单
                        ZhuanxianOrderRefuseEvent refuseEvent = new ZhuanxianOrderRefuseEvent(
                            pushMessage.getTargetId(), pushMessage.getEmployId(),
                            pushMessage.getMessage());
                        applicationEventMulticaster.multicastEvent(refuseEvent);
                    } else if (pushMessage.getPushType().equals(PushMessage.PUSHTYPE_CANCEL)) {//销单
                        ZhuanxianOrderCancelEvent cancelEvent = new ZhuanxianOrderCancelEvent(
                            pushMessage.getTargetId(), pushMessage.getEmployId(),
                            pushMessage.getFrom());
                        applicationEventMulticaster.multicastEvent(cancelEvent);
                    } else if (pushMessage.getPushType().equals(PushMessage.PUSHTYPE_BUDAN)) {//补单
                        ZhuanxianOrderAcceptEvent acceptEvent = new ZhuanxianOrderAcceptEvent(
                            pushMessage.getTargetId(), pushMessage.getEmployId());
                        applicationEventMulticaster.multicastEvent(acceptEvent);

                        ZhuanxianOrderRecordEvent recordEvent = new ZhuanxianOrderRecordEvent(
                            pushMessage.getTargetId());
                        applicationEventMulticaster.multicastEvent(recordEvent);
                    }

                }
            }

        } catch (Exception e1) {
            e1.printStackTrace();
        }

    }

}


